<?php
/**
 * Created by PhpStorm.
 * User: Duck
 * Create time: 2016/12/21 9:56
 */

namespace D\Controller;
use Common\Controller\BaseController;

class RoleController extends BaseController{
    public function show(){
        $pages = ceil(M('roles')->count()/4);
        $menu=M('menus')->where(array('up'=>array('neq','0')))->select();
        $a=M('roles')->select();
        $c=M('roles')->select();
        $this->assign('menuse',$menu);
        $this->assign('navs',$a);
        $this->assign('addnavs',$c);
        $this->assign('pages',$pages);
        $this->display();
    }
    public function getRole(){
        $where=[
          'role_id'=>I('get.id'),
        ];
        $res=M('roles')->where($where)->find();
        $ress=explode(',',$res['menu_ids']);
        $this->ajaxReturn($ress);
    }
    public function doDelRole(){
        M('roles')->delete(I('post.ids'));
        $res = [
            'msg'=>'删除成功',
            'error'=>0
        ];
        $this->ajaxReturn($res);
    }
    public function doEditRole(){
        if(D('roles')->create()){
            D('roles') ->save();
            $res = [
            'msg'=>'修改成功',
            'error'=>0
        ];
        }else{
            $res = [
                'msg'=>D('roles')->getError(),
                'error'=>1,
            ];
        }
        $this->ajaxReturn($res);
    }
    public function doAddRole(){
        if(D('roles')->create()){
            D('roles') ->add();
            $res = [
                'msg'=>'添加成功',
                'error'=>0
            ];
        }else{
            $res = [
                'msg'=>D('roles')->getError(),
                'error'=>1,
            ];
        }
        $this->ajaxReturn($res);
    }
}